package com.study.core.resp;

import com.study.core.error.ErrorCodeMsg;
import com.study.core.error.XXXErrorEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description 统一响应类
 * @Author dangxianyue
 * @Date 2024/5/31 09:49
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Resp<T> {

    private Integer code;
    private String msg;
    private T data;


    public static Resp ok() {
        return ok(null);
    }

    public static <T> Resp<T> ok(T data) {
        return resp(200, "OK", data);
    }

    public static Resp fail() {
        return fail(XXXErrorEnum.INTERNAL_SERVER_ERROR_500);
    }

    public static Resp fail(String msg) {
        return fail(500, msg);
    }

    public static Resp fail(int code, String msg) {
        return resp(code, msg, null);
    }

    public static Resp fail(ErrorCodeMsg errorCodeMsg) {
        return resp(errorCodeMsg.getCode(), errorCodeMsg.getMsg(), null);
    }

    public static  <T> Resp<T> resp(int code, String msg, T data) {
        return new Resp<>(code, msg, data);
    }

}
